package com.gemo.demo.controller;

import com.gemo.demo.entiy.News;
import com.gemo.demo.service.INewsService;
import com.gemo.demo.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private INewsService newsService;
    //删除
    @RequestMapping("/delete")
    public String delete(int id){
        newsService.deleteById(id);
        return "redirect:/news/list";
    }
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1",required = false) int pageNum,
                        String word,
                        @RequestParam(defaultValue = "0",required = false)int tid,
                       HttpSession session){
        PageInfo<News> pageInfo=newsService.getNews(tid,word,pageNum, PageUtil.PAGESIZE);
        session.setAttribute("pageInfo",pageInfo);
        session.setAttribute("currPageNum",pageNum);
        session.setAttribute("word",word);
        session.setAttribute("tid",tid);
        return "main";
    }
    //分页
    @RequestMapping("/index")
    public String index(@RequestParam(defaultValue = "1",required = false) int pageNum,
                         HttpSession session){
        int tid=Integer.parseInt(session.getAttribute("tid").toString());
        String word="";
        if(session.getAttribute("word")!=null){
            word=session.getAttribute("word").toString();
        }
        PageInfo<News> pageInfo=newsService.getNews(tid,word,pageNum, PageUtil.PAGESIZE);
        session.setAttribute("pageInfo",pageInfo);
        session.setAttribute("currPageNum",pageNum);
        return "main";
    }
}
